<template>
	<view class='login'>
		<view class='inp'>
			<view class='username'>
				<label>用户名:</label>
				<input type="text" placeholder="用户名" v-model="user_name" />
			</view>
			
			<view class="password">
				<label>密码:</label>
				<input type="text" placeholder="密码" v-model="password" password />
			</view>
			<view>
				<text style="font-size: 20px;">{{name2}}</text>
			</view>
		</view>
		<view class='button'>
			<button @click="login" ref="login">登录</button>
			<button @click="register" ref="register">注册</button>
		</view>
	</view>
</template>ie

<script>
export default{
	data(){
		return{
			user_name:'',
			password:'',
			id:'',
			name2:''
		}
	},
	// computed:{
	// 	userId(){
	// 	     this.$store.state.userId = this.id
	// 	}
	// },
	methods:{
		login(){
			this.name2=this.user_name
			// console.log("historyLists:",this.$store.state.historyLists)
			// console.log("userinfo:",this.$store.state.userinfo)
			// console.log("formData,",this.$store.state.formData)
			// console.log('login')
			let user ={
				"user_name":this.user_name,
				 "password":this.password
			}
			// this.$api.login({
			// 	user_name:user.user_name,
			// 	password:user.password
			// }).then((res)=>{
			// 	console.log(res)
			// 	if(data.status == 1){
			// 				uni.showToast({
			// 					title:data.msg,
			// 					icon:"none"
			// 				})
			// 				return
			// 			}else if(data.status == 2){
			// 				uni.showToast({
			// 					title:data.msg,
			// 					icon:"none"
			// 				})
			// 				return
			// 			}
			// 			uni.showToast({
			// 				title:data.msg,
			// 				icon:"none"
			// 			})
					
						
						
			// 			uni.reLaunch({
			// 		        // url:"../me/me?id="+this.id  //使用store  vue状态管理传参，避免url在页面地址栏暴漏
			// 				url:"../me/me"
			// 			})	
				
			// })
			uniCloud.callFunction({
				name:"login",
				data:user,
				success:(res)=>{
					const data = res.result;
					console.log('82-data:',data.userInfo.data)
					
					
					// this.$store.state.userId = data.userInfo.data[0]._id
					// console.log("this.$store.state.userId",this.$store.state.userId)
					
					if(data.status == 1){
					alert("data.status"+data.status)
						console.log('89-reLaunch======data.status:',data.status)
						
						uni.showToast({
							title:data.msg,
							icon:"none"
						})
						return
					}else if(data.status == 2){
					alert("data.status"+data.status)
						alert(data.msg)
						//用户信息持久化
						uni.showToast({
							title:data.msg,
							icon:"none"
						})
						
						return
					}
					// this.store.userId = data.userInfo.data[0]._id
					// console.log(this.store.userId)
					
					//#ifdef H5
					// alert(data.msg)
					//#endif
					uni.setStorage({
						key:"user",
						data:data.userInfo.data,
						success: () => {
							console.log(111)
							uni.showToast({
								title:data.msg,
								icon:"none"
							})
						}
					})
					
					uni.reLaunch({
				        // url:"../me/me?id="+this.id  //使用store  vue状态管理传参，避免url在页面地址栏暴漏
						url:"../me/me"
					})
					this.$store.commit('Get_id', data.userInfo.data[0]._id)
					console.log("this.$store.state.userId",this.$store.state.userId)
					//
					this.$store.commit('SET_USER_INFO', data.userInfo.data[0])
				}
			})
		},
		register(){
			console.log('去注册')
			uni.navigateTo({
				url:"../register/register"
			})
			
			// uni.navigateTo({
			// 	url:"../feedback/feedback"
			// })
			console.log('去注册2')
		}
	}
	}
</script>

<style lang="scss">
	.login{
		// margin:50upx 20upx;
		padding-top:100upx;
		
		.inp{
			// border:1px solid blue;
			display: flex; 
			flex-direction: column;
			align-items: center;
			margin: 80upx 0 80upx 0;
			font-size:20upx;

			.username,.password{
				 // text-align:center;
				 font-size:20px;
				 display:flex;
				  // align-items: center;
				 margin-bottom:30upx;

				 input{
					  padding-left: 25upx;
					  margin-left: 25upx;
					  border-bottom:1px solid black;
				 }
			}
		}
		.button{
			// border:1px solid blue;
			padding:0 10%;
			margin-top: 30%;
			button{
				margin-top:5px;
				background-color: #5ea666;
				color:#FFFFFF;
			}
		}
	}
</style>
